<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../node_modules/react/umd/react.development.js"></script>
    <script src="../node_modules/react-dom/umd/react-dom.development.js"></script>
    <script src="../node_modules/babel-standalone/babel.js"></script>
</head>
<body>
    <!-- 练习：两个组件，Parent, Child
            Parent host Child
            parent 负责确认用户的 id, 并且将 id 传递给 Child
            Child 根据 props 传入的 id, 访问后台数据，把获得的 name, age 显示出来 

        准备条件：3个data 文件
     -->
    <div id="app"></div>
    <script type="text/babel">
        class Parent extends React.Component {
            constructor(props) {
                super(props);

                this.state = {
                    id: 1
                }
            }

            next() {
                if(this.state.id < 3) {
                    this.setState({
                        id: this.state.id + 1
                    });
                }else {
                    this.setState({
                        id: 1
                    });
                }
            }

            render() {
                return (
                    <div>
                        <input type="button" value="Next" onClick={this.next.bind(this)} />
                        <Child id={this.state.id} />
                    </div>
                );
            }
        }

        class Child extends React.Component {
            constructor(props) {
                super(props);

                this.state = {
                    name: '',
                    age: ''
                }
            }

            shouldComponentUpdate(nextProps, nextState) {
                console.log(nextProps, nextState);

                // 要么，id 变了；要么，id 变完以后，触发 LoadData, 在 loadData 结束触发， name 变了
                return (
                    nextProps.id != this.props.id || nextState.name != this.state.name
                );

                // if(extProps.id != this.parops.id || nextState.name != this.state.name) {
                //     return true;
                // }else {
                //     return false;
                // }
            }

            loadData(id) {  // 请求数据
                fetch(`../data/data${id}.txt`).then(res => {
                    res.json().then(data => {
                        this.setState(data);
                    });
                });
            }

            componentDidMount() {
                console.log('did mount', this.props.id);

                this.loadData(this.props.id);
            }

            componentDidUpdate() {
                console.log('did update', this.props.id)
                this.loadData(this.props.id);
            }

            render() { // props state 改变都会触发
                console.log('child 渲染了');
                return (
                    <div>
                        <h2>ID: {this.props.id}</h2>
                        <p>用户名：{this.state.name}<br/>年龄：{this.state.age}</p>
                    </div>
                );
            }
        }

        ReactDOM.render(
            <Parent />,
            document.getElementById('app')
        );
    </script>
</body>
</html>